﻿package igz.fleaxy.ui.scrollbar
{
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import igz.fleaxy.ui.LinkUi;

	/**
	 * @author Intelygenz | www.intelygenz.com
	 */
	public class ScrollBarH extends ScrollBarAbstract {
		private const _DIRECTION_LEFT   : Boolean = false;
		private const _DIRECTION_RIGHT  : Boolean = true;
		
		private var _BtnLeft  : LinkUi;
		private var _BtnRight : LinkUi;
		
		/**
		 * Barra de scroll horizontal
		 * 
		 * @param	p_scrollContainerUi Contenedor scroll que controlará
		 * @param	p_styles 	: { backgroundStyle : "scrollbarBg", markStyle : "scrollbarMark" }
		 * @param	p_settings { 
						// backgroundStyle
						  "bgSize" : 10
						, "bgColor"	: 0xCBCBCB
						, "bgRound"	: [0, 0, 0, 0]
						, "bgAlpha"	: 1
						, "bgFilters" : []
						, "bgBorder" : null //Color del borde
						, "bgBorderAlpha" : 1
						, "bgBorderThickness" : 0
						// markStyle
						, "markSize" : 10
						, "markColor" : 0x0000FF
						, "markRound" : [0, 0, 0, 0]
						, "markAlpha" : 1
						, "markFilters" : []
						, "markBorder" : null //Color del borde
						, "markBorderAlpha" : 1
						, "markBorderThickness" : 0
						// other settings
						, "width" : null
						, "height" : null
						, "cssName" : null
						, "alwaysVisible" : false
						, "showArrows" : false
				        };
		 * @example
		 * 
		 * var container:ScrollContainerUi = new ScrollContainerUi();
		 * var scrollH:ScrollBarH = new ScrollBarH(container, "scrollBlue");
		 * 
		 * En la CSS por defecto:
		 * 	.scrollbarBg {
		 * 		igz-color: #212121;
		 * 		igz-round: 0,0,0,0;
		 * 		igz-size: 8; 
		 *  }
		 * 
		 *  .scrollbarMark {
		 * 		igz-color: #CBCBCB;
		 * 		igz-round: 0,0,0,0;
		 * 		igz-size: 10;
		 * 	}
		 */
		public function ScrollBarH( p_scrollContainerUi:ScrollContainerUi, p_styles:Object = null, p_settings:Object = null ) {
			super( p_scrollContainerUi, p_styles, p_settings );
			
			if ( _Settings["showArrows"] ) {
				_CreateArrowButtons();

				_TimerMove.addEventListener(TimerEvent.TIMER, _TimerMove_OnTimer);
			}
		}

		override protected function _Draw() : void {
			this.visible = _Settings["alwaysVisible"];

			var bgWidth:Number;
			if ( _Settings["showArrows"] ) {
				var buttonsSize : Number = _Settings["bgSize"] * 2;
				bgWidth =  _Settings["size"] == null ? _ScrollContainer.width -  buttonsSize: _Settings["size"] - buttonsSize;
				_Bg.x = _Settings["bgSize"];
				_BtnRight.x = bgWidth + _Settings["bgSize"];
				_Mark.x = _Settings["bgSize"];
			}
			else {
				bgWidth = _Settings["size"] == null ? _ScrollContainer.width : _Settings["size"];
			}
			
			_Bg.graphics.clear();
			_Bg.graphics.beginFill(_Settings["bgColor"], _Settings["bgAlpha"]);
			if ( _Settings["bgBorder"] != null &&  _Settings["bgBorderThickness"] > 0  ) {
				_Bg.graphics.lineStyle(_Settings["bgBorderThickness"], _Settings["bgBorder"], _Settings["bgBorderAlpha"], true);
			}
			var marginBorder : Number = _Settings["bgBorderThickness"] / 2;			
			_Bg.graphics.drawRoundRectComplex(marginBorder
											 ,marginBorder
											 , bgWidth - _Settings["bgBorderThickness"]
											 , _Settings["bgSize"] - _Settings["bgBorderThickness"]
											 , _Settings["bgRound"][0], _Settings["bgRound"][1]
											 , _Settings["bgRound"][2], _Settings["bgRound"][3]
											 );
			_Bg.graphics.endFill();
			
			_Mark.graphics.clear();

			if ( Math.floor(_ScrollContainer.$Container.width) > _ScrollContainer.width ) {
				_Mark.graphics.beginFill(_Settings["markColor"], _Settings["markAlpha"]);
				var markWidth:Number = (_ScrollContainer.width * _Bg.width) / _ScrollContainer.$Container.width;
				
				if ( _Settings["markBorder"] != null && _Settings["markBorderThickness"] > 0 ) {
					_Mark.graphics.lineStyle(_Settings["markBorderThickness"], _Settings["markBorder"], _Settings["markBorderAlpha"], true);
				}
				marginBorder = _Settings["markBorderThickness"] / 2;
				_Mark.graphics.drawRoundRectComplex(marginBorder
													, marginBorder
													, markWidth - _Settings["markBorderThickness"]
													, _Settings["markSize"] - _Settings["markBorderThickness"]
													, _Settings["markRound"][0], _Settings["markRound"][1]
													, _Settings["markRound"][2], _Settings["markRound"][3]
													);
				_Mark.graphics.endFill();

				_Mark.x = Math.abs( (_ScrollContainer.$Container.x * _Bg.width) / _ScrollContainer.$Container.width ) + _Bg.x;
				//_Mark.x = _Bg.x;
				_Mark.y = (_Settings["bgSize"] - _Settings["markSize"]) / 2;

				if ( _Mark.x + _Mark.width > _Bg.x + _Bg.width ) {
					//_Mark.x -= (_Mark.x + _Mark.width) - (_Bg.x + _Bg.width);
					_SetMarkX( _ScrollContainer.$Container.width );
				}
				
				this.visible = true;
				_Bg.addEventListener(MouseEvent.MOUSE_DOWN, _Bg_MouseDown);
			}
			else {
				_SetMarkX( 0 );
				_Bg.removeEventListener(MouseEvent.MOUSE_DOWN, _Bg_MouseDown);
			}
			
			if ( _Settings["autoPos"] ) {
				this.x = _ScrollContainer.x;
				this.y = _ScrollContainer.y + _ScrollContainer.height;
			}
			_CheckArrowButtons();
		}

		private function _SetMarkX(p_x:Number) : void {
			var x:Number = p_x;
			if ( x > _Bg.x
			  && x < _Bg.x + _Bg.width - _Mark.width
			) {
				_Mark.x = x;
			}
			else if ( x < _Mark.x ) {
				_Mark.x = _Bg.x;
			}
			else if ( x > _Mark.x ) {
				_Mark.x = _Bg.x + _Bg.width - _Mark.width;
			}
			x = -(((_Mark.x-_Bg.x) * _ScrollContainer.$Container.width) / _Bg.width);
			if ( isNaN(x) ) {
				x = 0;
			}
			_ScrollContainer.$Container.x = x;
			_CheckArrowButtons ();
		}

		
		override protected function _Mark_MouseMove (p_mouseEvent: MouseEvent) : void {
			_SetMarkX( p_mouseEvent.stageX - _Mark_OffsetX );
		}
		
		private function _Bg_MouseDown(p_mouseEvent:MouseEvent):void {
			p_mouseEvent.stopPropagation();
			
			var newMarkX : Number = p_mouseEvent.localX + _Bg.x;
			if ( newMarkX > _Mark.x ) {
				newMarkX -= _Mark.width;
			}
			_SetMarkX( newMarkX );
		}

		//
		// Botones de dirección
		//
		protected function _CreateArrowButtons () : void {
			var size : Number = _Settings["markSize"];
			var marginX : Number = size / 3;
			var marginY : Number = size / 5;
			
			// Draw left Arrow
			var shapeLeft :Shape = _CreateArrowButtonBg();
			shapeLeft.graphics.beginFill(_Settings["markBorder"]);
			shapeLeft.graphics.moveTo( size-marginX, marginY );
			shapeLeft.graphics.lineTo( size-marginX, size-marginY );
			shapeLeft.graphics.lineTo( marginX,size / 2);				
//			shapeLeft.graphics.lineTo( marginX, size / 2 );
//			shapeLeft.graphics.lineTo( size / 2, marginY );
			shapeLeft.graphics.endFill();
			
			// Draw right Arrow
			var shapeRight :Shape = _CreateArrowButtonBg();
			shapeRight.graphics.beginFill(_Settings["markBorder"]);
			shapeRight.graphics.moveTo( marginX, marginY);
			shapeRight.graphics.lineTo( marginX, size-marginY );			
			shapeRight.graphics.lineTo(size-marginX,size / 2 );

//			shapeRight.graphics.lineTo( marginX, marginY );
			shapeRight.graphics.endFill();
			
			_BtnLeft = new LinkUi( shapeLeft, { "onMouseDown": _BtnLeft_OnMouseDown
											, "onMouseUp": _BtnLeft_OnMouseUp
											, "onRollOut": _BtnLeft_OnRollOut }
								);
			addChild( _BtnLeft );
			
			_BtnRight = new LinkUi( shapeRight, { "onMouseDown": _BtnRight_OnMouseDown
												, "onMouseUp": _BtnRight_OnMouseUp
												, "onRollOut": _BtnRight_OnRollOut }
								);
			addChild( _BtnRight );
		}

		//chequeo de los botones de direccion
		private function _CheckArrowButtons () : void {
			if ( _Settings["showArrows"] ) {
				if ( Math.floor(_ScrollContainer.$Container.width) > _ScrollContainer.width ) {
					if ( _Mark.x <= _Bg.x ) {
						_BtnLeft.$Disable();
						_BtnRight.$Enable();
						_StopMove();
					}
					else if ( (_Mark.x + _Mark.width) >=  (_Bg.x + _Bg.width) ) {
						_BtnLeft.$Enable();
						_BtnRight.$Disable();
						_StopMove();
					}
					else {
						_BtnLeft.$Enable();
						_BtnRight.$Enable();
					}
				}
				else {
					_BtnLeft.$Disable();
					_BtnRight.$Disable();
				}
			}
		}		
		
		
		private function _BtnLeft_OnMouseDown ( p_mouseEvent:MouseEvent ) : void {
			_MoveDirection = _DIRECTION_LEFT;
			_Left();
			_TimerStartMove.start();
		}
		private function _BtnLeft_OnMouseUp ( p_mouseEvent:MouseEvent ) : void {
			_StopMove();
		}
		private function _BtnLeft_OnRollOut ( p_mouseEvent:MouseEvent ) : void {
			_StopMove();
		}

		private function _BtnRight_OnMouseDown ( p_mouseEvent:MouseEvent ) : void {
			_MoveDirection = _DIRECTION_RIGHT;
			_Right();
			_TimerStartMove.start();
		}
		private function _BtnRight_OnMouseUp ( p_mouseEvent:MouseEvent ) : void {
			_StopMove();
		}
		private function _BtnRight_OnRollOut ( p_mouseEvent:MouseEvent ) : void {
			_StopMove();
		}

		private function _TimerMove_OnTimer( p_timerEvent:TimerEvent ):void {
			if ( _MoveDirection == _DIRECTION_RIGHT ) {
				_Right();
			}
			else {
				_Left();
			}
		}

		private function _Right () : void {
			_SetMarkX( _Mark.x + _Settings["scrollSpeed"] );
		}

		private function _Left () : void {
			_SetMarkX( _Mark.x - _Settings["scrollSpeed"] );
		}

	}
	
}